package utils;

import java.util.ArrayList;
import java.util.List;

import javax.ejb.EJB;
import javax.ejb.LocalBean;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.TypedQuery;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Root;
import javax.persistence.metamodel.EntityType;
import model.Resource;
import dtos.ResourceDTO;

/**
 * @author Radulescu Cristina
 * @author Pop Loredana
 */
@Stateless
@LocalBean
public class ResourceDAO implements ResourceDAORemote {

    /**
     * Default constructor. 
     */
	@PersistenceContext
	EntityManager em;


	@EJB
	Conv conv;
	
    public ResourceDAO() {
        // TODO Auto-generated constructor stub
    }

    /**
     * Method used to insert resource
     * @param ra of type Resource
     */
    @Override
   	public void insertResource(ResourceDTO ra) {
    	Resource r=em.find(Resource.class, ra.getId());
    	
   		if(r==null){
   			Resource r1=conv.fromEntityResourceDTO(ra); 			
   			em.persist(r1);
   		}
   		else{
   			em.merge(r);
   		}
   		
   	}
    
    /**
     * Method used to update resource
     * @param Resource ud
     */
    @Override
    public void updateResource(ResourceDTO ud){
    	if(em.find(Resource.class, ud.getId())!=null)
    	  em.merge(conv.fromEntityResourceDTO(ud));
    }
    
    /**
     * Method used to get resource by id
     * @param id
     * @return resource
     */
    @Override
	public ResourceDTO getResource(int id) {
    	return conv.fromEntityResource(em.find(Resource.class, id));
	}
    
    /**
     * Method used to get all resource
     * @return list of resource
     */
    @Override
    public List<ResourceDTO> getAllResources() {
    	List<ResourceDTO> resultTicketsList = new ArrayList<>();
    	CriteriaBuilder cb = em.getCriteriaBuilder();
    	CriteriaQuery<Resource> cq = cb.createQuery(Resource.class);
    	Root<Resource> ticket = cq.from(Resource.class);
    	cq.select(ticket);
    	
    	TypedQuery<Resource> q = em.createQuery(cq);

		List<Resource> tickets = q.getResultList();
		for (Resource u : tickets) {
			resultTicketsList.add(conv.fromEntityResource(u));
		}
    	return resultTicketsList;
    }
    
    /**
     * Method used to delete Resource
     * @param resource
     */
    @Override
    public void deleteResource(ResourceDTO ud){
		Resource r = em.find(Resource.class, ud.getId());
        em.remove(r);
	}
    
    /**
     * Method used to get Resources by name
     * @param name
     * @return list of resources
     */
    @Override
    public List <ResourceDTO> getResourcesByName(String name) {
    	List<ResourceDTO> results = new ArrayList<>();
    	CriteriaBuilder cb = em.getCriteriaBuilder();
    	CriteriaQuery<Resource> cq = cb.createQuery(Resource.class);
    	EntityType<Resource> type = em.getMetamodel().entity(Resource.class);
    	Root<Resource> res = cq.from(Resource.class);
    	cq.select(res).where(cb.like(res.get(type.getDeclaredSingularAttribute("name",String.class)), "%"+name+"%"));
    	
    	TypedQuery<Resource> q = em.createQuery(cq);
		List<Resource> resour = q.getResultList();
		for (Resource u : resour) {
			results.add(conv.fromEntityResource(u));
		}
    	return results;
    }
    
}
